/*    file: WtlVideoWindow.h
 *    desc:
 *   
 * created: 2014-01-21 14:12:14
 *  author: zhengchuanjiang
 * company: 
 */ 


#if !defined WtlVideoWindow_H_
#define WtlVideoWindow_H_

#include "BasicType.h"
#include "VideoWindow.h"
#include "AVFramePtr.h"


#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif



#include <atlbase.h>
#include <atltypes.h>
#include <atlstr.h>

#include <atlapp.h>
#include <atlctrls.h>
#include <atlcrack.h>

#include <shellapi.h>
#include <atlctrlx.h>
#include <atlimage.h>

#include "TCriticalSection.h"
#include "HwndPresenter.h"
#include <deque>


////////////////////////////////////////////////////////////////////////////
namespace av
{



class WtlVideoWindow : public VideoWindow,
                        public CWindowImpl< WtlVideoWindow >

{
public:
    enum Constant
    {
        WM_DRAW_FRAME = WM_USER + 1,
        WM_SET_STATE,

    };

public:
    DECLARE_WND_CLASS("WtlVideoWindow")

    BEGIN_MSG_MAP_EX(WtlVideoWindow)
        MESSAGE_HANDLER(WM_PAINT, OnPaint)
        MSG_WM_ERASEBKGND(OnEraseBkgnd)
        MSG_WM_COMMAND(OnCommand)
        MSG_WM_SIZE(OnSize)
        MSG_WM_CREATE(OnCreate)
        MSG_WM_TIMER(OnTimer)
        MSG_WM_HSCROLL(OnHScroll)
        MESSAGE_HANDLER(WM_SET_STATE, OnSetState)
        MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
		MSG_WM_LBUTTONDOWN(OnLButtonDown)
		MSG_WM_LBUTTONUP(OnLButtonUp)
		MSG_WM_RBUTTONDOWN(OnRButtonDown)
		MSG_WM_RBUTTONUP(OnRButtonUp)
        MSG_WM_MOUSEMOVE(OnMouseMove)
        REFLECT_NOTIFICATIONS()
    END_MSG_MAP()

    WtlVideoWindow();
    ~WtlVideoWindow();

    virtual int open(void* hwnd);
    virtual void close();
    virtual bool isOpen();

    virtual void setVideoWnd(void* hwnd);

    virtual void setState(StreamState state);

    virtual bool draw(AVFrame* frame);

    virtual bool isFullscreen();

    virtual bool setFullscreen(bool full);

    virtual bool setRatio(int width, int height);

	virtual void setVisible(bool visible);

    virtual void getRatio(int& width, int& height);

    virtual bool setBackImage(const char* filepath);

    virtual void flush();


    virtual void onStateChanged(int state);

    virtual void onPositionChanged(int64_t pos);

    virtual void onScaleChanged(double scale);

    virtual void onVolumeChanged(double volume);

    virtual void onMuteChanged(bool muted);

    virtual void onFullscreenChanged(bool full);

    virtual void onRatioChanged(int width, int height);

protected:
    LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

    BOOL OnEraseBkgnd(HDC hdc);

    void OnSize(UINT nType, const CSize& size);

    LRESULT OnCreate(LPCREATESTRUCT lpcs);

    void OnHScroll(UINT nSBCode, UINT nPos, HWND hwnd);

    void OnTimer(UINT_PTR wParam);

    void OnCommand(UINT flags, int id, HWND hwnd);

    LRESULT OnSetState(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/);

    void OnLButtonDblClk(UINT flags, CPoint point);
	void OnLButtonDown(UINT flags, CPoint point);
	void OnLButtonUp(UINT flags, CPoint point);
	void OnRButtonDown(UINT flags, CPoint point);
	void OnRButtonUp(UINT flags, CPoint point);

    void OnMouseMove(UINT flags, CPoint point);

    void subclassWindow(HWND hwnd);
    void unsubclassWindow(HWND hwnd);

    static LRESULT CALLBACK MyWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

protected:

    void drawBackImage(HDC hdc);
    void drawBackground(HDC hdc);
    void drawFrame();

    void putFullScreen(bool full);

    void checkShowControl();

    bool histControl(int x, int y);

    void startControlTimer();
    void stopControlTimer();
    void onTimerControl();
    
    void onCommand(UINT flags, int id, HWND hwnd);
    void onHScroll(UINT code, UINT id);
    void updateUI(int state);
    void onSetState(int state);

    void startTrackTimer();
    void stopTrackTimer();
    void onTimerTrack();
    

    bool navigate(const char* url);

    void clearBuffer();
    void popFrameFromQueue();

    bool getVideoSize(int& width, int& height);
    CRect getVideoRect();

    void fetchFrame();

protected:
    HWND    m_parentWnd;
    int     m_ratioX;
    int     m_ratioY;
    bool    m_enableControl;

    comn::CriticalSection    m_cs;

    HwndPresenter   m_presenter;
    bool    m_fullscreen;

    RECT    m_rcLast;

    StreamState    m_state;

    AVFramePtr m_backFrame;
    AVFramePtr m_curFrame;

    CImage  m_backImage;

    WNDPROC m_superWindowProc;

};


}

////////////////////////////////////////////////////////////////////////////
#endif //WtlVideoWindow_H_

